package yyy.ab.common.converter;

import yyy.ab.common.utils.DateUtils;
import yyy.ab.common.utils.StringUtils;
import org.springframework.core.convert.converter.Converter;

import java.util.Date;

/**
 * @author liub
 * @Title: DateConverter
 * @Package com.water.converter
 * @Description: date converter
 * @date 2018/7/2 14:58
 */
public class DateConverter implements Converter<String, Date> {

    @Override
    public Date convert(@SuppressWarnings("NullableProblems") String s) {
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        if (StringUtils.isNumber(s)) {
            return new Date(Long.valueOf(s));
        } else  {
            if (s.length() > 10) {
                if (s.contains("GMT+0800")) {
                    return DateUtils.parseGMTTime(s);
                }
                return DateUtils.parseDate(s,"yyyy-MM-dd HH:mm:ss");
            } else if (s.length() == 10) {
                return DateUtils.parseDate(s,"yyyy-MM-dd");
            }
        }

        return null;
    }
}